package speedball.notice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketService {
    protected int port;
    protected  ServerSocket server;

    public SocketService(int port) {
        this.port = port;
    }


    //搭建服务器端
    public static void main(String[] args) throws IOException {
        SocketService socketService = new SocketService(5209);
        //1、a)创建一个服务器端Socket，即SocketService
        socketService.open();
        socketService.acceptDeal();
    }

    public void acceptDeal() {
        try {
//             final  Socket socket = null;
            try {
                while (true) {
//                    new Thread()
                    new Thread(()->{
                        try {
                            Socket socket = server.accept();

                        //3、获取输入流，并读取客户端信息
                        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        //在标准输出上打印从客户端读入的字符串
                        String line = in.readLine();

//            line=br.readLine();
                        //从标准输入读入一字符串
                        //4、获取输出流，响应客户端的请求
                        if (line.equals("start")) {
                            //在系统标准输出上打印读入的字符串
                            System.out.println("Client:" + "游戏启动");

                        } else if (line.equals("end")) {
                            System.out.println("Client:" + "游戏退出");
                        } else {
                            System.out.println("Client:未知指令");
                        }

                        //5、关闭资源

                            in.close(); //关闭Socket输入流

                            socket.close(); //关闭Socket
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }) ;

//                    invoke(socket);
                }

                //2、调用accept()方法开始监听，等待客户端的连接
                //使用accept()阻塞等待客户请求，有客户
                //请求到来则产生一个Socket对象，并继续执行
            } catch (Exception e) {
                System.out.println("Error." + e);
                //出错，打印出错信息
            }
            server.close(); //关闭ServerSocket/
        } catch (Exception e) {//出错，打印出错信息
            e.printStackTrace();
            System.out.println("Error." + e);
        }
    }

    public void close() {
        try {
            if (server != null) {
                server.close(); //关闭ServerSocket/
                System.out.println("服务器关闭成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("服务器关闭失败");
        }
    }

    public void open() {
        try {
            server = new ServerSocket(5209);
            //b)指定绑定的端口，并监听此端口。
            System.out.println("服务器启动成功");
            //创建一个ServerSocket在端口5209监听客户请求
        } catch (Exception e) {
            System.out.println("没有启动监听：" + e);
            //出错，打印出错信息
        }
    }

    //TODO
    public void portEmploy() {
        try {
            if (server != null) {
                server.getLocalPort();
            }

            server = new ServerSocket(5209);
            //b)指定绑定的端口，并监听此端口。
            System.out.println("服务器启动成功");
            //创建一个ServerSocket在端口5209监听客户请求
        } catch (Exception e) {
            System.out.println("没有启动监听：" + e);
            //出错，打印出错信息
        }
    }

    //TODO
    public boolean isOpening() {

        if (server!=null&&!server.isClosed()) {
            System.out.println(server.getLocalPort());
            return true;
        }
        return false;
    }
}
